GBML manual / Lists

list_foreach

Runs the script action once for each item in target, from the first to the last, handing it that item. action is the NAME of one of your scripts and it must take exactly one argument. It is the same as writing a loop over target that calls the script with target[i].

Syntax

list_foreach(target, action)

Arguments

NameType
targetlist
actionscript

Returns

Nothing.

Description

Runs a script once for each item in target, a list, from the first item to the last. Each time, the script is handed that item.

It does the same job as this loop, written once:

for (var i = 0; i < list_length(positions); i++) {
    spawn_coin(positions[i]);
}
list_foreach(positions, spawn_coin);

The script

The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that makes a coin at a place across the screen:

// scripts/spawn_coin.gbml
function spawn_coin(x: int) {
    instance_create(x, 20, obj_coin);
}

The script has to follow three rules, and each one is checked before the game runs:

A function written out in place

You do not have to make a script file for a short job. Write the function right where the name would go:

var sum = 0;
list_foreach(positions, function (x) { sum += x; });

It is the script, only without a name. Its one parameter takes the kind of the items of the list, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.

What happens in the awkward cases

You do thisWhat happens
give a function written out in the middle of the line, like function (x) { ... }that is fine: it is the script, only without a name (see "A function written out in place" above). It must take exactly one parameter, and it can use and change the variables around it
give the name of a variable, or of a built-in function like instance_destroyrefused when checked. It has to be one of your scripts
give a script with no parameters, or with two or morerefused when checked
give a script whose parameter is a different kind from the items, like a text parameter for a list of numbersrefused when checked, and the message says which kinds do not match
loop over a list with no itemsthe script is never called
let the script add to or take from the list it is being called fordo not rely on it. The loop looks at the length again before each item, so it can never read past the end, but what you get is hard to predict. Build a second list instead
give it an array or a gridrefused when checked, and the message names the function for that kind, like array_foreach
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes a list as a parameter works on the caller's own list (see Lists). So list_foreach inside a script loops over the caller's list, and it can be given any of your scripts to run.

What it costs

One call of your script for each item the list holds, and nothing else. No memory is set aside.

Example

var positions = list_create(3, 0);
list_add(positions, 20);
list_add(positions, 60);
list_add(positions, 100);
list_foreach(positions, spawn_coin);   // three coins, at x 20, 60 and 100

On the GBA

A plain loop over the list_length(target) items the list holds - never its capacity. The script is called once for each item, from the first place to the last, as an ordinary C++ call (`game::name(item)`) inside a loop that reads the length again every time round, so a script that changed the container it was walking could not make the loop read past the end. What the script gives back is ignored. The script is the name of one of your own scripts, or a function written out in the middle of the line (function (n) { ... }), which is written out as a C++ function where it stands and can see the variables around it; either way it must take exactly one argument whose type accepts the items (a function with none written takes the items' kind). It costs one call of the script per item and nothing else: no memory is set aside.

See also: array_foreachgrid_foreachlist_filterlist_lengthlist_addListsScriptsWriting a scriptData containers